using System.Linq;
using Magiq.Insert;
using Magiq.Support;

namespace Magiq.Providers.Objects
{
    public class InsertStrategy<TSource, TDestination, TNew> : IInsertStrategy
        where TDestination : class
        where TNew : class, TDestination, new()
    {
        private readonly IInsert<TSource, TDestination, TNew> insert;
        private readonly IInsertDestination<TNew> destination;

        public InsertStrategy(IInsert<TSource, TDestination, TNew> insert, IInsertDestination<TNew> destination)
        {
            this.insert = insert;
            this.destination = destination;
        }

        #region IInsertStrategy Members

        public int Execute()
        {
            var queryStrategy = MagiqConfigurator.ForInsertSourceObjects<TSource, TNew>(insert.Source);
            var items = queryStrategy.Execute().ToArray();
            destination.Insert(items);
            return items.Length;
        }

        #endregion
    }
}